import * as usersService from '../services/users';
import { message } from 'antd';

export default {
  namespace: 'users',
  state: {
    dict: [],
    list: [],
    total: null,
    page: null,
    result: {},
  },
  reducers: {
    list(state, { payload: { data: list, total, page } }) {
      return { ...state, list, total, page };
    },
    init(state, { payload: { dict } }) {
      return { ...state, dict };
    },
    modalerror(state, { payload: { result } }){
      return { ...state, result };
    },
  },
  effects: {
    *fetch({ payload: { page = 1 } }, { call, put }) {
      const { data, headers } = yield call(usersService.fetch, { page });
      yield put({
        type: 'list',
        payload: {
          data: data.content,
          total: parseInt(data.totalElements, 10),
          page: parseInt(page, 10),
        },
      });
    },
    *initDict({ payload: { page = 1, type = `key` } }, { call, put }) {
      const { data:dict } = yield call(usersService.getDict, { type });
      console.log(dict);
      yield put({
        type: 'init',
        payload: {
          dict: dict
        },
      });
    },
    *create({ payload: { values, callback } }, { call, put }) {
      const { data, type, message } = yield call(usersService.create, values);

      if (type === "success") {
        yield put({ type: 'reload' });
        yield call(callback);
        message.success('保存成功!', 3);
      } else {
        yield  put({ type: 'modalerror', payload: { result: { type: 'failure', messages: `保存失败:${message}` } } });
      }
    },
    *remove({ payload: id }, { call, put }) {
      yield call(usersService.remove, id);
      yield put({ type: 'reload' });
    },
    *patch({ payload: { id, values, callback } }, { call, put }) {
      const { data, type, message } = yield call(usersService.patch, { id, values });
      if (type === "success") {
        yield put({ type: 'reload' });
        yield call(callback);
        message.success('修改成功!', 3);
      } else {
        yield  put({ type: 'modalerror', payload: { result: { type: 'failure', messages: `修改失败:${message}` } } });
      }

    },
    *reload(action, { put, select }) {
      const page = yield select(state => state.users.page);
      yield put({ type: 'fetch', payload: { page } });
    },
    *query({ payload: values }, { call, put, select }) {
      const page = yield select(state => state.users.page)
      const { data, headers } = yield call(usersService.query, { values, page });
      yield put({
        type: 'list',
        payload: {
          data: data.content,
          total: parseInt(data.totalElements, 10),
          page: parseInt(page, 10),
        },
      });
    },
  },
  subscriptions: {
    init({ dispatch, history }) {
      return history.listen(({ pathname, query }) => {
        if (pathname === '/users') {
          dispatch({ type: 'initDict', payload: query });
        }
      });
    },
    setups({ dispatch, history }) {
      return history.listen(({ pathname, query }) => {
        if (pathname === '/users') {
          dispatch({ type: 'fetch', payload: query });
        }
      });
    },
  },
};
